/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.web.controller;

import cc.altius.domain.CustomUserDetails;
import cc.altius.domain.Password;
import cc.altius.service.UserService;
import cc.altius.utils.DateUtils;
import cc.altius.utils.LogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

/**
 *
 * @author Akil Mahimwala
 */
@Controller
@RequestMapping("/admin/updateExpiredPassword.htm")
@SessionAttributes("password")
public class UpdateExpiredPasswordController {

    @Autowired
    private UserService userService;

    @RequestMapping(method = RequestMethod.GET)
    public String showForm(ModelMap model) {
        Password password = new Password();
        CustomUserDetails curUser = (CustomUserDetails) (SecurityContextHolder.getContext().getAuthentication().getPrincipal());
        LogUtils.debug("Showing form for UpdateExpiredPassword");
        password.setUserId(curUser.getUserId());
        password.setUsername(curUser.getUsername());
        model.addAttribute("password", password);
        return "/admin/updateExpiredPassword";
    }

    @RequestMapping(method = RequestMethod.POST, params="_update")
    private String updatePassword(final @ModelAttribute("password") Password password, Errors errors, SessionStatus sessionStatus) {
        if (!this.userService.confirmPassword(password)) {
            LogUtils.debug("Nested path="+errors.getNestedPath());
            errors.rejectValue("oldPassword", "password.notMatch");
            return "/admin/updateExpiredPassword";
        } else {
            LogUtils.debug("Updating user password");
            this.userService.updatePassword(password, 90);
            // all you need to do now is load the correct Authorities
            Authentication curAuthentication = SecurityContextHolder.getContext().getAuthentication();
            CustomUserDetails curUser = (CustomUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

            curUser.setExpiresOn(DateUtils.getOffsetFromCurrentDateObject(DateUtils.IST, 90));
            curUser.setBusinessFunction(this.userService.getBusinessFunctionsForUserId(curUser.getUserId()));
            UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(curUser, curAuthentication.getCredentials(), curUser.getAuthorities());
            auth.setDetails(curAuthentication.getDetails());
            SecurityContextHolder.getContext().setAuthentication(auth);
            curAuthentication = SecurityContextHolder.getContext().getAuthentication();
            sessionStatus.setComplete();
            return "redirect:/customer/searchCustomerCommonHeader.htm?msg=msg.passwordUpdated";
        }
    }
}
